package com.hk.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.apache.solr.util.ArraysUtils;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.DataMasking;
import com.hk.commons.HqlFilter;
import com.hk.commons.LongId;
import com.hk.commons.entity.Points;
import com.hk.commons.entity.UserExtraInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IPointsHistory;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.IPoints;
import com.hk.service.sys.ISysUser;

@Service
@Aspect
public class UserExtraInfoImpl extends BaseServiceImpl<UserExtraInfo> implements
		IUserExtraInfo {

	private Logger logger = LoggerFactory.getLogger(UserExtraInfoImpl.class);

	@Autowired
	IPointsHistory pointsHistoryImpl;

	@Autowired
	IPoints pointsImpl;

	@Autowired
	ISysUser userService;

	@Override
	public void deleteById(Long id) {
		UserExtraInfo model = findById(id);
		delete(model);
	}

	@Override
	public void delete(UserExtraInfo u) {

	}

	@Override
	public UserExtraInfo findById(long id) {

		return getById(id);
	}

	@Override
	public Long add(UserExtraInfo model) {
		Long id = LongId.get();
		model.setId(id);
		model.setCts(System.currentTimeMillis());
		super.save(model);
		return id;
	}

	@Override
	public Boolean addRelationUserAmount(Long userId, Long finUserId) {
		logger.info("UserExtraInfoImpl -- addRelationUserAmount userId = " + userId + "  finUserId = " + finUserId);
		SysUser user = userService.findById(userId);
		if (user != null) {
			UserExtraInfo info = findByUserId(finUserId);
			if (info != null) {
				if (info.getRelationUserAmount() != null) {
					info.setRelationUserAmount(info.getRelationUserAmount() + 1);
				} else {
					info.setRelationUserAmount(1);
				}

				update(info);
			} else {
				info = new UserExtraInfo();
				info.setRelationUserAmount(1);
				info.setUserId(finUserId);
				add(info);
			}
			
			/*
			String msg = pointsImpl.getAddUserMsg(userId);
			
			logger.info("UserExtraInfoImpl -- addRelationUserAmount will add points "
					+ " msg  = " + msg);
			
			addPoints(finUserId, ConstantPointRule.POINT_RULE01_ADD_USER, msg);
			*/
		}
		return true;
	}

	@Override
	public Boolean addTieCardAmount(Long userId, Long finUserId) {
		logger.info("UserExtraInfoImpl -- addTieCardAmount in"
				+ " userId  = " + userId + "  finUserId = " + finUserId);
		UserExtraInfo info = findByUserId(finUserId);
		if (info != null) {
			if (info.getTieCardAmount() != null) {
				info.setTieCardAmount(info.getTieCardAmount() + 1);
			} else {
				info.setTieCardAmount(1);
			}

			update(info);
		} else {
			info = new UserExtraInfo();
			info.setTieCardAmount(1);
			info.setUserId(userId);
			add(info);
		}
		
		/*
		String msg = pointsImpl.getAddTieCardMsg(userId);
		
		logger.info("UserExtraInfoImpl -- addTieCardAmount will add points "
				+ " msg  = " + msg);
		
		addPoints(finUserId, ConstantPointRule.POINT_RULE02_ADD_TIECARD, msg);
		*/
		return true;
	}

	@Override
	public Boolean addBespeakOrderAmount(Long bespeakOrderId, Long userId) {
		logger.info("UserExtraInfoImpl -- addBespeakOrderAmount in"
				+ " bespeakOrderId  = " + bespeakOrderId + "  finUserId = " + userId);
		
		UserExtraInfo info = findByUserId(userId);
		if (info != null) {
			info.setBespeakOrderAmount(info.getBespeakOrderAmount() + 1); 
			this.update(info);
		} else {
			info = new UserExtraInfo();
			info.setBespeakOrderAmount(1);
			info.setUserId(userId);
			this.add(info);
		}
		/*
		//如果符合条件，则增加积分
		CheckResultMsg checkResult = pointsImpl.checkActive(ConstantPointRule.POINT_RULE03_ADD_BESPEAK, finUserId);
		if(checkResult.getResult()){
			String msg = pointsImpl.getAddBespeakOrderMsg(bespeakOrderId);

			logger.info("UserExtraInfoImpl -- addBespeakOrderAmount will add points "
				+ " msg  = " + msg);
		
			addPoints(finUserId, ConstantPointRule.POINT_RULE03_ADD_BESPEAK, msg);
		}
		*/
		return true;
	}

	@Override
	public Boolean addPayOrderAmount(Long orderId, BigDecimal amount, Long finUserId)  {
		logger.info("UserExtraInfoImpl -- addPayOrderAmount in"
				+ " orderId  = " + orderId + "  finUserId = " + finUserId);
		UserExtraInfo info = findByUserId(finUserId);
		if (info != null) {
			if (info.getPayOrderAmount() != null) {
				info.setPayOrderAmount(info.getPayOrderAmount() + 1);
				info.setPayOrderMoney(info.getPayOrderMoney().add(amount));
			} else {
				info.setPayOrderAmount(1);
			}

			update(info);
		} else {
			info = new UserExtraInfo();
			info.setPayOrderAmount(1);
			info.setUserId(finUserId);
			info.setPayOrderMoney(amount);
			add(info);
		}
		
		/*
		String msg = pointsImpl.getAddPayOrderMsg(orderId);
		
		logger.info("UserExtraInfoImpl -- addPayOrderAmount will add points "
				+ " msg  = " + msg);
		
		addPoints(finUserId, ConstantPointRule.POINT_RULE04_ADD_ORDER, msg);
		*/
		return true;
	}
	
	
	@Override
	public UserExtraInfo findByUserId(long userId) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("userId", userId);
		UserExtraInfo info = getByFilter(hqlFilter);
		return info;
	}

	@Override
	public Boolean addPoints(Long finUserId, Integer pointsType, String msg) {
		
		logger.info("UserExtraInfoImpl -- addPoints in finUserId = "
				+ finUserId +  "  pointsType = " + pointsType
				+ " msg  = " + msg);
		
		UserExtraInfo info = findByUserId(finUserId);
		Points pointRule = pointsImpl.getPoinitsByType(pointsType);
	
		Integer pointsVal = pointsImpl.getVal(pointsType);
		// 如果info 为空则新增
		if (info == null && pointRule != null) {
			info = new UserExtraInfo();
			info.addPoints(pointsVal);
			info.setUserId(finUserId);
			this.add(info);
		}
		
		
		if (info != null && pointRule != null) {
			info.addPoints(pointsVal);
			this.update(info);
		} else {
			logger.error("UserExtraInfo info = null or points = null");
		}

		// 添加积分记录信息
		pointsHistoryImpl.add(finUserId, pointsType, msg);
		
		return true;
	}
	
	@Override
	public Boolean addPoints(Long finUserId, Integer pointsType) {
		
		logger.info("UserExtraInfoImpl -- addPoints in finUserId = "
				+ finUserId +  "  pointsType = " + pointsType);
		
		UserExtraInfo info = findByUserId(finUserId);
		Points pointRule = pointsImpl.getPoinitsByType(pointsType);
	
		Integer pointsVal = pointsImpl.getVal(pointsType);
		// 如果info 为空则新增
		if (info == null && pointRule != null) {
			info = new UserExtraInfo();
			info.addPoints(pointsVal);
			info.setUserId(finUserId);
			this.add(info);
		}
		
		
		if (info != null && pointRule != null) {
			info.addPoints(pointsVal);
			this.update(info);
		} else {
			logger.error("UserExtraInfo info = null or points = null");
		}

		// 添加积分记录信息
		pointsHistoryImpl.add(finUserId, pointsType, pointRule.getActDesc());
		
		return true;
	}
	
	
	@Override
	public Integer getLeftPointsByUserId(Long userId) {
		
		logger.info("UserExtraInfoImpl -- getLeftPointsByUserId in userId = " + userId);
		
		UserExtraInfo info = findByUserId(userId);
		Integer leftPoints = info.getLeftPoints();
		
		return leftPoints;
	}
	
	@Override
	public String[] getTopTieCardUser(Integer num){
		String [] nameArr = new String[5];
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addSort("tieCardAmount");
		hqlFilter.addOrder("desc");
		List<UserExtraInfo> listInfos = findByFilter(hqlFilter);
		for(int i=0; i<num; i++){
			UserExtraInfo info = listInfos.get(i);
			SysUser user = userService.findById(info.getUserId());
			if(user != null){
				nameArr[i] = DataMasking.getMaskingData( user.getName(),  4, 4);
			}
		}
		return nameArr;
	}
}
